package jmind.dolphin.frame;

import jmind.base.util.Objects;
import jmind.dolphin.redis.Command;
import jmind.dolphin.util.Const;

import java.io.Serializable;
import java.util.Arrays;


/**
 * client command
 * @author Created by xieweibo on 2016/11/28.
 */
public class DolphinCommand implements Serializable {
	// 命令
	private  CommandType command;
	// 服务名
	private String serviceName;
	// 参数
	private String params ;
	// set 的值
	private String value;

	public DolphinCommand(byte[] key,byte[] val){
        serviceName=Objects.toString(key);
        int index=serviceName.indexOf(Const.QUESTION_MARK);
        if(index>0) {
            params=serviceName.substring(index+1);
            serviceName = serviceName.substring(0, index);
        }
        if(val!=null)
        value=new String(val);
    }

	public DolphinCommand(Command redisCommand) {
		Object[] commands = redisCommand.objects;
		try{
		if(commands.length>0){
			command=CommandType.valueOf(Objects.toString(commands[0]).toUpperCase());
		}
		if(commands.length>1){
			serviceName=Objects.toString(commands[1]);
			int index=serviceName.indexOf(Const.QUESTION_MARK);
			if(index>0) {
				params=serviceName.substring(index+1);
				serviceName = serviceName.substring(0, index);
			}
		}
		if(commands.length>2){
			value=Objects.toString(commands[2]);
		}
		}catch (Exception e){
			value=Arrays.toString(redisCommand.objects);
		}

	}

	public CommandType getCommand() {
		return command;
	}


	public String getServiceName() {
		return serviceName;
	}


	public String getParams() {
		return params;
	}


	public String getValue() {
		return value;
	}

	public String getFullName(){
		return serviceName+"-"+command.name();
	}

}
